package com.wiscamp.ninechapters.panels.application.handlers;

import com.wiscamp.ninechapters.panels.domain.events.ApplicationCreatedEvent;
import com.wiscamp.ninechapters.panels.domain.services.ApplicationService;
import com.wiscamp.ninechapters.panels.domain.services.UserService;
import cube.ddd.aspect.HandleServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ApplicationCreatedEventHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private final ApplicationService applicationService;

    private final UserService userService;

    @Autowired
    public ApplicationCreatedEventHandler(ApplicationService applicationService, UserService userService) {
        this.applicationService = applicationService;
        this.userService = userService;
    }

    @Async
    @HandleServiceException
    @EventListener(ApplicationCreatedEvent.class)
    public void onApplicationEvent(ApplicationCreatedEvent event) {
        long appId = event.getApplicationId();

        var app = applicationService.getApplication(appId);
        logger.warn(app.toString());
        // todo: 创建应用管理员账号
    }
}
